package com.kunya.product.service.impl;

import com.kunya.common.common.ResponseCode;
import com.kunya.common.common.ServerResponse;
import com.kunya.product.dao.specification.SpecificationAttributeMapper;
import com.kunya.product.po.SpecificationAttribute;
import com.kunya.product.service.specofocation.ISpecificationAttributeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author WangGang
 * @Description: 规格属性接口实现类
 * @date 2018/7/17 16:53
 */
@Service("iSpecificationAttributeService")
public class SpecificationAttributeServiceImpl implements ISpecificationAttributeService {
    @Autowired
    private SpecificationAttributeMapper specificationAttributeMapper;

    /**
     * 添加规格属性
     * @param specificationAttribute
     * @return
     */
    @Override
    public ServerResponse addSpecificationAttribute(SpecificationAttribute specificationAttribute,String tablePrefix){

        if(specificationAttribute.getSpecificationId() ==  null || StringUtils.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        specificationAttribute.setCreateBy("hnsj001");
        specificationAttribute.setCreateTime(new Date());
        List<SpecificationAttribute> list = specificationAttributeMapper.selectBySpecificationId(specificationAttribute.getSpecificationId(),tablePrefix);
        for (SpecificationAttribute specificationAttribute1 : list){
            if (specificationAttribute1.getName().equals(specificationAttribute.getName())){
                return ServerResponse.createByErrorMessage("属性已存在！");
            }
        }
        specificationAttributeMapper.insertSelective(specificationAttribute,tablePrefix);
        return ServerResponse.createByErrorMessage("插入成功！");
    }

    /**
     * 修改规格属性
     * @param specificationAttribute
     * @param tablePrefix
     * @return
     */
    @Override
    public ServerResponse updateSpecificationAttribute(SpecificationAttribute specificationAttribute,String tablePrefix){
        if(specificationAttribute.getSpecAttrId() ==  null || StringUtils.isEmpty(tablePrefix)){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        if (specificationAttribute.getName() == null){
            return ServerResponse.createByErrorMessage("name不能为空!");
        }
        SpecificationAttribute specificationAttribute1 = new SpecificationAttribute();
        specificationAttribute1.setSpecAttrId(specificationAttribute.getSpecAttrId());
        specificationAttribute1.setName(specificationAttribute.getName());
        int num = specificationAttributeMapper.updateByPrimaryKeySelective(specificationAttribute1,tablePrefix);
        if (num <= 0){
            return ServerResponse.createByErrorMessage("修改失败");
        }else {
            return ServerResponse.createBySuccessMessage("修改成功");
        }
    }

    @Override
    public ServerResponse deleteSpecificationAttribute(SpecificationAttribute specificationAttribute,String tablePrefix){
        if (specificationAttribute.getSpecAttrId() == null){
            return ServerResponse.createByErrorMessage("specAttrId不能为空!");
        }
        try {
            specificationAttributeMapper.deleteByPrimaryKey(specificationAttribute.getSpecAttrId(),tablePrefix);
            return ServerResponse.createBySuccessMessage("删除成功");
        }catch (Exception  e){
            return ServerResponse.createByErrorMessage("删除失败");
        }
    }

    @Override
    public ServerResponse selectSpecificationAttribute(SpecificationAttribute specificationAttribute,String tablePrefix){
        if (specificationAttribute.getSpecificationId() == null){
            return ServerResponse.createByErrorMessage("specificationId不存在");
        }
        List<SpecificationAttribute> list = specificationAttributeMapper.selectBySpecificationId(specificationAttribute.getSpecificationId(),tablePrefix);
        return ServerResponse.createBySuccess(list);
    }

}
